import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  Req,
  Res,
  Inject,
  Query,
  BadRequestException,
  HttpStatus,
} from '@nestjs/common';
import { Aaa4Service } from './aaa4.service';
import { CreateAaa4Dto } from './dto/create-aaa4.dto';
import { UpdateAaa4Dto } from './dto/update-aaa4.dto';
import { SessionService } from '../session/session.service';
import { Request, Response } from 'express';
import { IsPublic } from '../is-public.decorator';
import { RedisService } from '../redis/redis.service';
import { ApiBasicAuth, ApiBearerAuth, ApiBody, ApiCookieAuth, ApiOperation, ApiParam, ApiQuery, ApiResponse, ApiTags } from '@nestjs/swagger';
import { TestSwagger3Vo } from './vo/testSwagger3.vo';
import { TestSwagger3Dto } from './dto/testSwagger3.dto';

@ApiTags('aaa4')
@Controller('aaa4')
export class Aaa4Controller {
  constructor(private readonly aaa4Service: Aaa4Service) {}

  @Inject(SessionService)
  private sessionService: SessionService;

  @Inject(RedisService)
  private redisService: RedisService;

  @IsPublic()
  @Get('count')
  async count(@Req() req: Request, @Res({ passthrough: true }) res: Response) {
    const sid = req.cookies?.sid;
    const session = await this.sessionService.getSession<{ count: string }>(
      sid,
    );

    const curCount = session.count ? parseInt(session.count) + 1 : 1;
    const curSid = await this.sessionService.setSession(sid, {
      count: curCount,
    });
    res.cookie('sid', curSid, { maxAge: 1800000 });
    return curCount;
  }

  @IsPublic()
  @Get('addPos')
  async addPos(
    @Query('name') posName: string,
    @Query('longitude') longitude: number,
    @Query('latitude') latitude: number,
  ) {
    if (!posName || !longitude || !latitude) {
      throw new BadRequestException('位置信息不全');
    }
    try {
      await this.redisService.geoAdd('positions', posName, [
        longitude,
        latitude,
      ]);
    } catch (error) {
      throw new BadRequestException(error.message);
    }
    return {
      message: '添加成功',
      statusCode: 200,
    };
  }

  @IsPublic()
  @Get('allPos')
  async allPos() {
    return this.redisService.geoList('positions');
  }

  @IsPublic()
  @Get('pos')
  async pos(@Query('name') name: string) {
    return this.redisService.geoPos('positions', name);
  }

  @IsPublic()
  @Get('nearBySearch')
  async nearbySearch(
    @Query('longitude') longitude: number,
    @Query('latitude') latitude: number,
    @Query('radius') radius: number,
  ) {
    if(!longitude || !latitude) {
      throw new BadRequestException('缺少位置信息');
    }
    if(!radius) {
      throw new BadRequestException('缺少搜索半径');
    }
    return this.redisService.geoSearch('positions', [longitude, latitude], radius);
  }


  @ApiBearerAuth('bearer')
  @ApiOperation({summary: '测试 swagger', description: 'swagger 描述'})
  @ApiResponse({
    status: HttpStatus.OK,
    description: 'aaa成功',
    type: String
  })
  @ApiQuery({
    name: 'a1',
    type: String,
    description: 'a1 param',
    required: true,
    example: '111'
  })
  @ApiQuery({
    name: 'a2',
    type: String,
    description: 'a2 param',
    required: false,
    example: '222'
  })
  @IsPublic()
  @Get('testSwagger')
  testSwagger(@Query('a1') a1, @Query('a2') a2) {
    console.log(a1, a2)
    return 'test swagger'
  }

  @ApiCookieAuth('cookie')
  @ApiParam({
    name: 'id',
    description: 'id',
    required: true,
    example: '12121'
  })
  @IsPublic()
  @Get('testSwagger2/:id')
  testSwagger2(@Param('id') id: number) {
    console.log(id)
    return 'test swagger2'
  }
  
  @ApiBasicAuth('basic')
  @ApiResponse({
    status: HttpStatus.OK,
    description: '成功了啊',
    type: TestSwagger3Vo
  })
  @ApiBody({
    type: TestSwagger3Dto
  })
  @IsPublic()
  @Get('testSwagger3')
  testSwagger3() {
    const vo = new TestSwagger3Vo()
    vo.aaa = 111
    vo.bbb = 222
    return vo
  }
}
